//
//  UIRing.m
//  AccuroFit
//_lblMeasVal
//  Created by Yongfeng Li on 8/5/15.
//  Copyright (c) 2015 Yongfeng Li. All rights reserved.
//

#import "UIRingMeter.h"
#import "AppDelegate.h"
#import "ViewController.h"
#define SHeight  [UIScreen mainScreen].bounds.size.height
#define RFont    SHeight / 12.5
@implementation UIRingMeter {
  UILabel* _lblPedoBattery;
  UILabel* _lblMeasVal;
  UILabel* _lblUnitTxt;
  //UILabel* _lblBodyBmi;
  UILabel* _lblFatVal;
    
  //other
  UILabel* _lblGoalWgt;
  UILabel* _lblWgtLost;
  UILabel* _lblToGoal;
    
  UILabel* _lblAvgHrate;
  UILabel* _lblBestBldp;
    
  UILabel* _lblTodayDists;
  UILabel* _lblGoalSteps;
  UILabel* _lblMostSteps;
  
  UITextField* _txtFatInput;
  UITextField* _txtWgtInput;
  short _nLastMeasWgtUn;
    
  UIImageView* _ivRing;
  UIImageView* _ivBott;
    
  int _nMeasTypeIdx;
    
  UILabel* _lblLastInput;
  NSString* _sLastLabelTxt;
  id _txtDelegate;
  
  NSString* _aMeasVal;
  
  //hrm background round
  //CALayer* _hrmBkgdLayer;
  CAShapeLayer* _hrmCircleLayer;
  BOOL _bIpad;
  BOOL _bIp6p;
  BOOL _bIp6;
  CGRect _rtGoalWgt;
  
  BOOL _bCancel;
}
//@synthesize m_lblLastInput =_lblLastInput;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //self.backgroundColor = [UIColor colorWithWhite:0.05 alpha:0.95];
        self.backgroundColor = [UIColor clearColor];
        //self.backgroundColor = [UIColor redColor];
        
        //BOOL bIpad = frame.size.width > 500;
        //BOOL bWider = frame.size.height >= 310;
        
        /*if(!bIpad) {
            //UIImage* iBkgd = [[UIImage imageNamed:(bIpad?@"ic-topbkgd-b.png":@"ic-topbkgd-s.png")] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage* iBkgd = [UIImage imageNamed:(bIpad?@"ic-topbkgd-b.png":@"ic-topbkgd-s.png")];
            _ivBott = [[UIImageView alloc] initWithFrame:CGRectMake(bIpad?0:0, frame.size.height - (bIpad?20:60), frame.size.width-(bIpad?0: 0), bIpad?20:40)];
            [_ivBott setImage: iBkgd];
            if(bIpad) {
                _ivBott.alpha = 0.620;
                //_ivBott.backgroundColor = [UIColor colorWithWhite:0.25 alpha:0.95];
            }
            else if(bWider) { // 4 inch and above
                _ivBott.alpha = 0.875;
                //_ivBott.backgroundColor = [UIColor colorWithWhite:0.75 alpha:1.00];
            }
            else {
                _ivBott.alpha = 0.785;
                //_ivBott.backgroundColor = [UIColor colorWithWhite:0.75 alpha:1.00];
            }
            [self addSubview:_ivBott];
        }*/
        
      CGFloat fScale = (frame.size.height > 585)? 1.0: 1.1;
        
        CGRect rt = CGRectMake(0, 0, frame.size.width * fScale, frame.size.height * fScale);
        //if(rt.size.height <= 324)
          //rt.origin.y -= 14;
        _ivRing = [[UIImageView alloc]initWithFrame:rt];
        _ivRing.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_ivRing];
        _ivRing.backgroundColor = [UIColor clearColor];
      
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFrame:) name:@"UpdateLableFrame" object:nil];
        
        [self initialize: frame];
    }
    _nMeasTypeIdx = 3;
    //[self setRingImage:_nMeasTypeIdx];
    //self.backgroundColor = [UIColor redColor];
    _lblLastInput = _lblMeasVal;
    _sLastLabelTxt = @"";
  
  //hrm backgroun color fill
  /*_hrmBkgdLayer = [CALayer layer];
  _hrmBkgdLayer.frame = frame;
  //_hrmBkgdLayer.delegate = self;
  [self.layer addSublayer:_hrmBkgdLayer];*/
  _hrmCircleLayer = nil;
  _bCancel = NO;
  
  return self;
}

-(void)updateFrame:(NSNotification *)notification {
    
    UIButton *btn = (UIButton *)notification.object;
    
    CGFloat originX = btn.origin.x - btn.width / 100 * 21;
    
    
//    _lblAvgHrate.x = originX;

    
    _lblWgtLost.x = originX;
    _lblWgtLost.width = btn.width;
    
    _lblGoalSteps.x = originX;
    _lblGoalSteps.width = btn.width;
    
    _lblGoalWgt.width = btn.width;
    _lblGoalWgt.x =  originX + btn.width / 100 * 18;
    _lblGoalWgt.y -= 5;
    
    _lblToGoal.width = btn.width;
    _lblToGoal.centerX = _lblGoalWgt.centerX;
    _lblToGoal.y -= 3;
    
    _lblTodayDists.width = btn.width;
    _lblTodayDists.centerX = _lblGoalWgt.centerX;
    _lblTodayDists.y += 6;
    
    _lblMostSteps.width = btn.width;
    _lblMostSteps.centerX = _lblGoalWgt.centerX;
    
    _lblAvgHrate.width = _lblToGoal.width;
    _lblAvgHrate.center = _lblToGoal.center;
    
    //    _lblBestBldp.x = originX;
    _lblBestBldp.width = _lblToGoal.width;
    _lblBestBldp.center = _lblToGoal.center;
//    _lblMostSteps.y += 5;
}

- (void) initialize:(CGRect)frame
{
  //CGRect rtDev = [UIDevice currentDevice].accessibilityFrame;
  CGRect rtFrame = [[UIScreen mainScreen] bounds];
  CGFloat fScale = [[UIScreen mainScreen] scale];
  //CGFloat W = rtFrame.size.width;
  CGFloat H = rtFrame.size.height;
  
  BOOL bIpad =  H > 1000;
  BOOL bIp6p = fScale >= 3;
  BOOL bIp6 = fScale == 2 && H == 568;//frame.size.height > 381;
  BOOL bIp4s = H == 480;
  if(bIp4s)
    bIp6 = NO;
  _bIpad = bIpad;
  if(!_bIpad)
    _bIp6p = bIp6p;
  if(!_bIp6p)
    _bIp6 = bIp6;
  if(!bIpad && !bIp6p && !bIp6)
    bIp4s = YES;
    
  CGFloat xPos = bIpad?(frame.size.width /2+60): (frame.size.width /2-60);
  CGFloat yPos = frame.size.height/2 - (bIpad?10:15);
    if(frame.size.width <= 320) {
        xPos -= 3;
        yPos -= 9;
    }
    
  _lblPedoBattery = [[UILabel alloc] initWithFrame:CGRectMake(xPos-50, yPos-60, 280, 80)];
  _lblPedoBattery.textColor = [UIColor grayColor];
  _lblPedoBattery.font = [UIFont systemFontOfSize: bIpad? 16:12];
  _lblPedoBattery.backgroundColor = [UIColor clearColor];
  _lblPedoBattery.textAlignment = NSTextAlignmentCenter;
  _lblPedoBattery.text = @"";
  //[_lblMsg setText:@"WiFi Scale setup step 1/3"];//[NSString stringWithFormat:@". %@",NSLocalizedString(@"CurtUser", nil)]];
  [self addSubview:_lblPedoBattery];
  _lblPedoBattery.hidden = YES;
  
  _lblMeasVal = [[UILabel alloc] initWithFrame:CGRectMake(xPos, yPos, 380, SHeight / 6)];
  //_lblMeasVal.textColor = [UIColor blackColor];
  if(_nMeasTypeIdx == 0)
    _lblMeasVal.textColor = [UIColor colorWithRed:112.0/255 green:42.0/255 blue:135.0/255 alpha:1.0];
  else if(_nMeasTypeIdx == 1)
     _lblMeasVal.textColor = [UIColor colorWithRed:215.0/255 green:26.0/255 blue:117.0/255 alpha:1.0];
  else if(_nMeasTypeIdx == 2)
    _lblMeasVal.textColor = [UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:1.0];
  else if(_nMeasTypeIdx == 3)
    _lblMeasVal.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
  //_lblMeasVal.hidden = YES;
 
  if(_nMeasTypeIdx == 0)
    _lblMeasVal.font = [UIFont boldSystemFontOfSize:bIpad? 72: RFont];
  else
    _lblMeasVal.font = [UIFont boldSystemFontOfSize:bIpad? 52: RFont];
  
    _lblMeasVal.font = Bold(bIpad? 52: 120);
    _lblMeasVal.backgroundColor = [UIColor clearColor];
    _lblMeasVal.textAlignment = NSTextAlignmentCenter;
    _lblMeasVal.text = @"---";
    //[_lblMsg setText:@"WiFi Scale setup step 1/3"];//[NSString stringWithFormat:@". %@",NSLocalizedString(@"CurtUser", nil)]];
    [self addSubview:_lblMeasVal];
    //_lblMeasVal.backgroundColor = [UIColor blueColor];
  
  _lblUnitTxt = [[UILabel alloc] initWithFrame:CGRectMake(xPos-20, yPos + 50, 280, 40)];
  _lblUnitTxt.textColor = _lblMeasVal.textColor;//[UIColor whiteColor];
  _lblUnitTxt.font = [UIFont systemFontOfSize: bIpad? 20:bIp4s?10:bIp6p?17:bIp6?16:15];
  _lblUnitTxt.backgroundColor = [UIColor clearColor];
  _lblUnitTxt.textAlignment = NSTextAlignmentCenter;
  _lblUnitTxt.text = @"kg";
  //[_lblMsg setText:@"WiFi Scale setup step 1/3"];//[NSString stringWithFormat:@". %@",NSLocalizedString(@"CurtUser", nil)]];
  [self addSubview:_lblUnitTxt];
  _lblUnitTxt.hidden = YES;
  
  _txtFatInput = [[UITextField alloc] initWithFrame:CGRectMake(xPos+(bIpad?-40:32), yPos-(bIpad?-30: 22), (bIpad?180: 120), (bIpad?40: 30))];
  _txtFatInput.textColor = _lblMeasVal.textColor;//[UIColor blackColor];
  _txtFatInput.backgroundColor = [UIColor whiteColor];
  _txtFatInput.font = [UIFont systemFontOfSize:bIpad?17:14];
  _txtFatInput.borderStyle = UITextBorderStyleRoundedRect;
  _txtFatInput.layer.cornerRadius = 12;
  _txtFatInput.keyboardType = UIKeyboardTypeEmailAddress;
  _txtFatInput.placeholder = @"Enter Fat (%)"; //NSLocalizedString(@"Name", nil);
  _txtFatInput.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  [self addSubview: _txtFatInput];
  _txtFatInput.hidden = YES;
  
  _txtWgtInput = [[UITextField alloc] initWithFrame:CGRectMake(xPos+(bIpad?-40:32), yPos+(_bIpad?-30:0), (bIpad?180: 120), (bIpad?40: 30))];
  _txtWgtInput.textColor = _lblMeasVal.textColor;//[UIColor blackColor];
  _txtWgtInput.backgroundColor = [UIColor whiteColor];
  _txtWgtInput.font = [UIFont systemFontOfSize:bIpad?17:14];
  _txtWgtInput.borderStyle = UITextBorderStyleRoundedRect;
  _txtWgtInput.layer.cornerRadius = 12;
  _txtWgtInput.keyboardType = UIKeyboardTypeEmailAddress;
  _txtWgtInput.placeholder = @"Enter Wgt."; //NSLocalizedString(@"Name", nil);
  _txtWgtInput.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  [self addSubview: _txtWgtInput];
  _txtWgtInput.hidden = YES;
    
    /*xPos = bIpad? 112: 45;
    yPos = bIpad? frame.size.height-160:frame.size.height-70;
    if(frame.size.width <= 320) {
        xPos -= 10;
        yPos += 10;
    }*/
  xPos += bIpad? 30: (bIp6p?90:bIp6?89:52);
  yPos -= bIpad? 145: (bIp6p?35:bIp6?35:25);
    if(frame.size.height <= 150)
        yPos += 8;
  _lblFatVal = [[UILabel alloc] initWithFrame:CGRectMake(xPos, yPos, 100, 30)];
  _lblFatVal.textColor = _lblMeasVal.textColor;//[UIColor whiteColor];
    _lblFatVal.font = Regular(SHeight / 31);
  _lblFatVal.backgroundColor = [UIColor clearColor];
  _lblFatVal.textAlignment = NSTextAlignmentCenter;
  _lblFatVal.text = @"--%";
    _lblFatVal.centerX = _lblMeasVal.centerX + CGRectGetWidth(_ivRing.frame) / 30;
  //[_lblMsg setText:@"WiFi Scale setup step 1/3"];//[NSString stringWithFormat:@". %@",NSLocalizedString(@"CurtUser", nil)]];
  [self addSubview:_lblFatVal];
    
  //goal wgtg
  CGFloat fIpadFontSize = bIpad?20:13;
  CGFloat fIphoneFontSize = bIpad?18:12;
  xPos -= bIpad? 340:bIp6p?135:bIp6?150:(bIp4s?95:95);
  yPos -= bIpad? 40:bIp6p?-5:bIp6?15:(bIp4s?50:37);
//  _rtGoalWgt = CGRectMake(xPos-3, yPos, 120, 30);
    _rtGoalWgt = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds) / 13, CGRectGetHeight(_ivRing.frame) / 4, 100, 30);
  _lblGoalWgt = [[UILabel alloc] initWithFrame:_rtGoalWgt];
  _lblGoalWgt.textColor = _lblMeasVal.textColor;//[UIColor whiteColor];
//  _lblGoalWgt.font = [UIFont boldSystemFontOfSize: (bIpad? fIpadFontSize:fIphoneFontSize)];
    _lblGoalWgt.font = Bold(SHeight / 34);
  _lblGoalWgt.backgroundColor = [UIColor clearColor];
  _lblGoalWgt.textAlignment = NSTextAlignmentCenter;
  _lblGoalWgt.text = @"----";
    _lblGoalWgt.centerX -= 7;
    _lblFatVal.centerY = _lblGoalWgt.centerY + SHeight / 40;
  _lblGoalWgt.userInteractionEnabled = YES;
  UITapGestureRecognizer *tapWgtGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onlabelGoalWgtTap)];
  [_lblGoalWgt addGestureRecognizer:tapWgtGesture];
  [self addSubview:_lblGoalWgt];
  _lblGoalWgt.hidden = YES;
    
    //wgt lost
  xPos -= bIpad?50:bIp6p?32:bIp6?18:(bIp4s?22:22);
  yPos += bIpad?170:bIp6p?65:bIp6?82:(bIp4s?62:62);
    //if(frame.size.height <= 150)
        //yPos += 10;
//  _lblWgtLost = [[UILabel alloc] initWithFrame:CGRectMake(xPos-5, yPos-18, 120, 30)];
    _lblWgtLost = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_ivRing.frame) / 12 * 5 + 4, 120, 30)];
  _lblWgtLost.textColor = _lblMeasVal.textColor;//[UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:0.75];
  
//    _lblWgtLost.font = [UIFont boldSystemFontOfSize: (bIpad? fIpadFontSize:fIphoneFontSize)];
    _lblWgtLost.font = Bold(SHeight / 23.5);
    _lblWgtLost.backgroundColor = [UIColor clearColor];
    _lblWgtLost.textAlignment = NSTextAlignmentCenter;
    _lblWgtLost.text = @"----";
    [self addSubview:_lblWgtLost];
    
    //toGoal
  xPos += bIpad?135:bIp6p?10:bIp6?8:0;
  yPos += bIpad?165:bIp6p?75:bIp6?80:(bIp4s?40:40);
    //if(frame.size.height <= 150)
        //yPos += 10;
//    _lblToGoal = [[UILabel alloc] initWithFrame:CGRectMake(xPos, yPos-30, 120, 30)];
    _lblToGoal = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_ivRing.frame) / 3 * 2 - 15, 120, 30)];
  _lblToGoal.textColor = _lblMeasVal.textColor;//[UIColor whiteColor];
//    _lblToGoal.font = [UIFont boldSystemFontOfSize: (bIpad? fIpadFontSize:fIphoneFontSize)];
    _lblToGoal.font = Bold(SHeight / 31);
    _lblToGoal.backgroundColor = [UIColor clearColor];
    _lblToGoal.textAlignment = NSTextAlignmentCenter;
    _lblToGoal.text = @"----";
    _lblGoalWgt.centerX = _lblWgtLost.centerX + CGRectGetWidth(_ivRing.frame) / 15;
    _lblToGoal.centerX = _lblWgtLost.centerX + CGRectGetWidth(_ivRing.frame) / 15;
    [self addSubview:_lblToGoal];
    
    //avg hrate
  xPos -= bIpad?-0:(bIp6p?20:bIp6?18:(bIp4s?16:16));
  yPos += bIpad?0:bIp6p?-18:bIp6?-22:(bIp4s?-24:-24);
    //if(frame.size.height <= 150)
        //yPos += 10;
//  _lblAvgHrate = [[UILabel alloc] initWithFrame:CGRectMake(xPos, yPos, 120, 30)];
    _lblAvgHrate = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_ivRing.frame) / 3 * 2 - 15, 120, 30)];
  _lblAvgHrate.textColor = _lblMeasVal.textColor;//[UIColor colorWithRed:112.0/255 green:42.0/255 blue:135.0/255 alpha:0.75];
//    _lblAvgHrate.font = [UIFont boldSystemFontOfSize: (bIpad? fIpadFontSize:fIphoneFontSize)];
     _lblAvgHrate.font = Bold(SHeight / 31);
    _lblAvgHrate.backgroundColor = [UIColor clearColor];
    _lblAvgHrate.textAlignment = NSTextAlignmentCenter;
    _lblAvgHrate.text = @"---";
    [self addSubview:_lblAvgHrate];
   
    //best bdp
  xPos -= bIpad? 0:bIp6p?-8:bIp6?-6:(bIp4s?-4:-4);
  yPos += bIpad?15:bIp6p?10:bIp6?8:(bIp4s?6:6);
    //if(frame.size.height <= 150)
        //yPos += 10;
//    _lblBestBldp = [[UILabel alloc] initWithFrame:CGRectMake(xPos-8, yPos-18, 120, 30)];
    _lblBestBldp = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_ivRing.frame) / 3 * 2 - 15, 120, 30)];
  _lblBestBldp.textColor = _lblMeasVal.textColor;
//    _lblBestBldp.font = [UIFont boldSystemFontOfSize: (bIpad? fIpadFontSize:fIphoneFontSize)];
    _lblBestBldp.font = Bold(SHeight / 31);
    _lblBestBldp.backgroundColor = [UIColor clearColor];
    _lblBestBldp.textAlignment = NSTextAlignmentCenter;
    _lblBestBldp.text = @"-----";
    [self addSubview:_lblBestBldp];
    
    //diatance
  xPos += bIpad?35:bIp6p?0:_bIp6?0:(bIp4s?0:0);
  yPos -= bIpad?350:bIp6p?123:bIp6?120:(bIp4s?118:118);
    //if(frame.size.height <= 150)
        //yPos += 10;
//    _lblTodayDists = [[UILabel alloc] initWithFrame:CGRectMake(50, yPos, 120, 30)];
    _lblTodayDists = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(_ivRing.frame) / 30, CGRectGetHeight(_ivRing.frame) / 5, 100, 30)];
  _lblTodayDists.textColor = _lblMeasVal.textColor;//[UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:0.75];
    _lblTodayDists.font = Bold(SHeight / 35.7);
    _lblTodayDists.backgroundColor = [UIColor clearColor];
    _lblTodayDists.textAlignment = NSTextAlignmentCenter;
    _lblTodayDists.userInteractionEnabled = YES;
    UITapGestureRecognizer* tapDistGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onlabelGoalDistTap)];
    [_lblTodayDists addGestureRecognizer:tapDistGesture];
    _lblTodayDists.text = @"----";
    _lblTodayDists.centerY = _lblTodayDists.centerY + 7;
    [self addSubview:_lblTodayDists];
    
    //steps to goal
  xPos -= bIpad? 40: bIp6p?5:bIp6?4:(bIp4s?3:3);
  yPos += bIpad? 168:bIp6p?45:bIp6?44:(bIp4s?35:35);
    //if(frame.size.height <= 150)
        //yPos += 10;
//    _lblGoalSteps = [[UILabel alloc] initWithFrame:CGRectMake(xPos, yPos-10, 120, 30)];
    _lblGoalSteps = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 2 - 15, 120, 30)];
  _lblGoalSteps.textColor = _lblMeasVal.textColor;//[UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:0.75];
    _lblGoalSteps.font = Bold(SHeight / 23.5);
    _lblGoalSteps.backgroundColor = [UIColor clearColor];
    _lblGoalSteps.textAlignment = NSTextAlignmentCenter;
    _lblGoalSteps.userInteractionEnabled = YES;
    _lblGoalSteps.centerX = _lblGoalSteps.centerX - 8;
    UITapGestureRecognizer* tapStepGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onlabelGoalStepTap)];
    [_lblGoalSteps addGestureRecognizer:tapStepGesture];
    _lblGoalSteps.text = @"----";
    [self addSubview:_lblGoalSteps];
    
    //most steps
  xPos += bIpad? 40:bIp6p?15:bIp6?13:(bIp4s?10:10);
  yPos += bIpad? 135:bIp6p?53:bIp6?55:(bIp4s?42:42);
    //if(frame.size.height <= 150)
        //yPos += 10;
//    _lblMostSteps = [[UILabel alloc] initWithFrame:CGRectMake(xPos, yPos, 120, 30)];
    _lblMostSteps = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 3 * 2, 120, 30)];
  _lblMostSteps.textColor = _lblMeasVal.textColor;//[UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:0.75];
    _lblMostSteps.font = Bold(SHeight / 35.7);
    _lblMostSteps.backgroundColor = [UIColor clearColor];
    _lblMostSteps.textAlignment = NSTextAlignmentCenter;
    _lblMostSteps.text = @"-----";
    _lblTodayDists.centerX = _lblGoalSteps.centerX + CGRectGetWidth(_ivRing.frame) / 15;
    _lblMostSteps.centerX = _lblGoalSteps.centerX + CGRectGetWidth(_ivRing.frame) / 15;
    [self addSubview:_lblMostSteps];
}

- (ViewController *)parentViewController {
  UIResponder *responder = self;
  while ([responder isKindOfClass:[UIView class]])
    responder = [responder nextResponder];
  return (ViewController *)responder;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

/*- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
  if(_nMeasTypeIdx != 0)
    return;
  
  CGFloat x = 130;
  CGFloat y = 150;
  CGContextAddArc(ctx, x, y, 100.0, 0, 2 * M_PI, 0); // Or is it 2 * M_PI?
  
  CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
  //CGContextSetStrokeColorWithColor(ctx, strokeColor);
  CGContextDrawPath(ctx, kCGPathFillStroke);
  
}*/

//- (void) drawRect:(CGRect)rect
//{
  /*if(_nMeasTypeIdx != 0)
    return;
  
  CGContextRef ctx = UIGraphicsGetCurrentContext();
     
  CGFloat x = 130;
  CGFloat y = 150;
  //CGContextAddArc(ctx, x, y, 100.0, 0, 2 * M_PI, 0); // Or is it 2 * M_PI?
    
  //CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
  //CGContextSetStrokeColorWithColor(ctx, strokeColor);
  //CGContextDrawPath(ctx, kCGPathFillStroke);*/
  
  
    
    // Create gradient
    /*CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = {0.0, 1.0};
    
    UIColor *centerColor = [UIColor orangeColor];
    UIColor *edgeColor = [UIColor purpleColor];
    
    NSArray *colors = [NSArray arrayWithObjects:(__bridge id)centerColor.CGColor, (__bridge id)edgeColor.CGColor, nil];
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
    
    // Scaling transformation and keeping track of the inverse
    CGAffineTransform scaleT = CGAffineTransformMakeScale(2, 1.0);
    CGAffineTransform invScaleT = CGAffineTransformInvert(scaleT);
    
    // Extract the Sx and Sy elements from the inverse matrix
    // (See the Quartz documentation for the math behind the matrices)
    CGPoint invS = CGPointMake(invScaleT.a, invScaleT.d);
    
    // Transform center and radius of gradient with the inverse
    CGPoint center = CGPointMake((self.bounds.size.width / 2) * invS.x, (self.bounds.size.height / 2) * invS.y);
    CGFloat radius = (self.bounds.size.width / 2) * invS.x;
    
    // Draw the gradient with the scale transform on the context
    CGContextScaleCTM(ctx, scaleT.a, scaleT.d);
    CGContextDrawRadialGradient(ctx, gradient, center, 0, center, radius, kCGGradientDrawsBeforeStartLocation);
    
    // Reset the context
    CGContextScaleCTM(ctx, invS.x, invS.y);
    
    // Continue to draw whatever else ...
    
    // Clean up the memory used by Quartz
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);*/
    
    //CGRect rt = CGRectInset(rect, 20, 20);
    //[self drawRing:ctx frame:rt];
//}

- (void) setRingImage:(int) nMeasType
{
  _nMeasTypeIdx = nMeasType;
  NSString* imgName = @"";
  
  //_bIpad = self.frame.size.width >= 648;
  //if(!_bIpad)
    //_bIp6p = self.frame.size.height > 420;
  float fScale = 1.1;
  if(nMeasType == 0) {
      imgName = NSLocalizedString(@"ring-hrate", nil); //@"ring-hrate.png";
    _lblMeasVal.font = [UIFont boldSystemFontOfSize: _bIpad? 52: RFont];
  }
  else  if(nMeasType == 1) {
    imgName = NSLocalizedString(@"ring-bldpre", nil);//@"ring-bldpre.png";
    _lblMeasVal.font = [UIFont boldSystemFontOfSize: _bIpad? 72: RFont];
  }
  else if(nMeasType == 2) {
    fScale = 1.2;
    imgName = NSLocalizedString(@"ring-steps", nil);//@"ring-steps.png";
    _lblMeasVal.font = [UIFont boldSystemFontOfSize: _bIpad? 72: RFont];
    /*CGRect rt = self.frame;
    rt.size.width *= 1.05;
    rt.size.height *= 1.05;
    _ivRing.frame = rt;*/
  }
  else {
    fScale = 1.2;
    imgName = NSLocalizedString(@"ring-wgts", nil);//@"ring-wgts.png";
    _lblMeasVal.font = [UIFont boldSystemFontOfSize: _bIpad? 72: RFont];
    /*CGRect rt = self.frame;
    rt.size.width *= 1.05;
    rt.size.height *= 1.05;
    _ivRing.frame = rt;*/
  }
    _lblMeasVal.font = [UIFont boldSystemFontOfSize: _bIpad? 72: RFont];
  if(_bIpad)
    fScale = 1.0;
  
  AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  //UIImage* iRing = [UIImage imageNamed:imgName];
  UIImage* iRing = [pApp getImageScale:imgName scale:fScale];
  //iRing = [iRing imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  [_ivRing setImage:iRing];
  _ivRing.alpha = 1.0;
  
  //if(nMeasType == 0) {
    //_ivRing.highlightedImage = [UIImage imageNamed:@"btn-left.png"];
  //}
  //_ivRing.highlighted = nMeasType == 0;
  
  if((_nMeasTypeIdx >= 2)) {
    CGRect rtLeftTop = _rtGoalWgt;
    rtLeftTop.origin.x -= _bIpad?20:_bIp6p?20:_bIp6?6:4;
    rtLeftTop.origin.y -= _bIpad?22:_bIp6p?22:_bIp6?4:4;
//      if(_nMeasTypeIdx == 2){
//          _lblTodayDists.frame = rtLeftTop;
//      }
//    else
//      _lblGoalWgt.frame = rtLeftTop;
  }
  
  CGRect rt = self.frame;
  NSUInteger W = rt.size.width;
  BOOL bIpad = _bIpad; //rt.size.width > 500;
  BOOL bIp6 = _bIp6; //rt.size.height > 381;
  
  CGFloat xPos = (rt.size.width / 2) - (bIpad?(65): bIp6?40: 36);
  CGFloat yPos = rt.size.height/2 - (bIpad?50:(bIp6?24:21));
  CGFloat fWid =  bIpad? 280: 180;
  CGFloat fHgt = bIpad? 80: 50;
  if(rt.size.width <= 320) {
    xPos -= 6;
    yPos -= (W<= 225)?17: 12;
  }
  if(nMeasType == 2) {
    xPos += 10;
    yPos += 0;
  }
  else if(nMeasType==3) {
    xPos += 15;
    yPos += -5;
  }
  
  yPos += _bIp6?15:_bIp6p?18:0;
  
    BOOL bIp4s = self.frame.size.height < 275;
//    _lblMeasVal.frame = CGRectMake(xPos-(_bIpad?10:5), yPos+(_bIpad?0:0) - SHeight / 15, 280,SHeight / 6);
        _lblMeasVal.frame = CGRectMake(CGRectGetWidth(_ivRing.frame) * 0.75 - SHeight / 5, yPos+(_bIpad?0:0) - SHeight / 15, CGRectGetWidth(_ivRing.frame) / 1.8,SHeight / 6);
    _lblMeasVal.backgroundColor = [UIColor clearColor];
    _lblMeasVal.frame = CGRectMake(_lblMeasVal.width / 40 + _lblMeasVal.frame.origin.x, _lblMeasVal.frame.origin.y - 8, _lblMeasVal.frame.size.width, _lblMeasVal.frame.size.height);
    _lblMeasVal.centerY = _lblWgtLost.centerY;
    _lblFatVal.centerX = _lblMeasVal.centerX + CGRectGetWidth(_ivRing.frame) / 7.48;
  //_lblMeasVal.text = @"Wgt Lb";
//    _lblMeasVal.backgroundColor = [UIColor redColor];
  if(nMeasType == 3)
    _lblUnitTxt.frame = CGRectMake(xPos+(_bIpad?50:_bIp6p?25:20), yPos+(_bIpad?90:_bIp6p?43:_bIp6?48:bIp4s?35:40), fWid/2, fHgt/2);
  else
    _lblUnitTxt.text = @"";
    
  [self hideOthCtrls:nMeasType != 3];
    
  _lblAvgHrate.hidden =  nMeasType != 0;
  _lblBestBldp.hidden = nMeasType != 1;
    
  _lblTodayDists.hidden = nMeasType != 2;
  _lblGoalSteps.hidden = nMeasType != 2;
  _lblMostSteps.hidden = nMeasType != 2;
  //[self loadLastData];
  
  BOOL bBatteryHide = nMeasType != 2;
  _lblPedoBattery.hidden = bBatteryHide;
}

- (void) onlabelGoalWgtTap
{
  [self setManualMode: YES];
  _lblLastInput = _lblGoalWgt;
  _sLastLabelTxt = _lblGoalWgt.text;
  
  //_txtFatInput.placeholder = @"Goal Fat.(%)";
  _txtFatInput.hidden = YES;
  _txtWgtInput.placeholder = @"Goal Wgt.";
  _lblGoalWgt.text = @"???";
}

- (void) onlabelGoalDistTap
{
    if(_lblLastInput == _lblGoalSteps) {
        _lblGoalSteps.text = _sLastLabelTxt;
        //_lblGoalSteps.hidden = NO;
    }
    [self setManualMode: YES];
    _lblLastInput = _lblTodayDists;
    _sLastLabelTxt = _lblTodayDists.text;
    _txtWgtInput.placeholder = @"Goal Dist.(mi)"; 
    _lblTodayDists.text = @"???";
}

- (void) onlabelGoalStepTap
{
    if(_lblLastInput == _lblTodayDists) {
        _lblTodayDists.text = _sLastLabelTxt;
        //_lblGoalSteps.hidden = NO;
    }
    [self setManualMode: YES];
    _lblLastInput = _lblGoalSteps;
    _sLastLabelTxt = _lblGoalSteps.text;
    _txtWgtInput.placeholder = @"Goal Steps"; 
    _lblGoalSteps.text = @"???";
}

/*- (void) loadLastData
{
    if(_nMeasTypeIdx == 0) {
        _lblMeasVal.text = @"114";
    }
    else if(_nMeasTypeIdx == 1) {
        _lblMeasVal.text = @"110/70";
    }
    else if(_nMeasTypeIdx == 2) {
        _lblMeasVal.text = @"4,500";
    }
    else {
        _lblMeasVal.text = @"162.3";
    }
}*/

- (void) setManualMode:(BOOL)bInput
{
  if(!bInput && _txtFatInput.hidden && _txtWgtInput.hidden) {
    if(_lblLastInput != nil && _bCancel) {
      _lblLastInput.text = _sLastLabelTxt;
      _bCancel = NO;
    }
    _lblUnitTxt.text = (_nLastMeasWgtUn == 1)? @"kg": @"kg";
    return;
  }
  
  _lblFatVal.hidden = bInput;
  _lblMeasVal.hidden = bInput;
  
  _txtFatInput.hidden = !bInput;
  _txtWgtInput.hidden = !bInput;
  if(bInput) {
    _txtFatInput.delegate = self;
    _txtWgtInput.delegate = self;
    _txtFatInput.text = @"";
    _txtWgtInput.text = @"";
    [_txtFatInput becomeFirstResponder];
    
     AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    _lblUnitTxt.text = (pApp.m_bodyInfo.nWgtUn == 1)? @"kg": @"kg";
  }
  else {
    _txtFatInput.delegate = nil;
    _txtWgtInput.delegate = nil;
    _txtWgtInput.placeholder = @"Enter Wgt.";
    if(_lblLastInput != nil && _bCancel) {
      _lblLastInput.text = _sLastLabelTxt;
      _bCancel = NO;
    }
    
     _lblUnitTxt.text = (_nLastMeasWgtUn == 1)? @"kg": @"kg";
  }
}

-(float) insertFatInputData:(NSString*)sTxtVal
{
  AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  LoginBodyInfo* bodyInfo =pApp.m_bodyInfo;
  float fFatVal = sTxtVal.floatValue;
  if(fFatVal > 60)
    fFatVal = 60;
  
  if([pApp insertFatData:bodyInfo.m_nBodyId fat:fFatVal wat:0.0 hgt:0.0 unit:0 oth:0 date:[NSDate date] toSvr:YES] != nil)
    return fFatVal;
  else
    return 0.0f;
}

-(BOOL) insertGoalData:(NSString*)sTxtVal
{
    BOOL bOK = NO;
    AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LoginBodyInfo* bodyInfo = pApp.m_bodyInfo;
    
    if(_lblLastInput == _lblMeasVal) {
      float fEntWgt = sTxtVal.floatValue;
      Byte nInputWgtUn = bodyInfo.nWgtUn;
      
      NSString* sDate = [pApp.dtFmt stringFromDate:[NSDate date]];
      bOK = [pApp insertWgtData:@"enter" card:@"input" data:fEntWgt unit:bodyInfo.nWgtUn bodyId:pApp.m_bodyInfo.m_nBodyId dtScale:sDate iOStime:YES toSvr:YES] != NULL;
      
      float fGoalWgt = bodyInfo.fGoalWgt;
      float fIniWgt = bodyInfo.fBodyWgt;
      float fCurWgt = fEntWgt;
      float fLwgt = roundf((fIniWgt - fCurWgt) * 10) / 10.0;
      float fSwgt = roundf ((fIniWgt - fGoalWgt) * 10) / 10.0;
      float fToGol = fLwgt * 100 / fSwgt;
      float fWgtLoss = (fEntWgt>0)?fLwgt:0;
      
      //bmi update
      
      
      short nHgtMm = bodyInfo.nHgtMm;
      float fBmi = (fEntWgt / nHgtMm / nHgtMm) * 1000000.0;
      if(nInputWgtUn == 2)
        fBmi *= _LbtoKg;
      ViewController* pvCtrl = [self parentViewController];
      
      _lblWgtLost.text =[NSString stringWithFormat:@"%.1f %@", fWgtLoss, (nInputWgtUn==1)?@"kg": @"kg"];
      _lblToGoal.text = [NSString stringWithFormat:@"%.1f", fToGol];
      _lblMeasVal.text = sTxtVal;
      
      [pvCtrl updateBodyWgtInfo:0.0 wgt:fBmi];
    }
    else if(_lblLastInput == _lblGoalWgt) {
      //NSString* sGoal = _lblMeasVal.text;
      float fGoalWgt = sTxtVal.floatValue;
      float fIniWgt = pApp.m_bodyInfo.fBodyWgt;
      float fCurWgt = _lblMeasVal.text.floatValue;
      float fToGol = (fIniWgt - fCurWgt) * 100 / (fIniWgt - fGoalWgt);
      _lblToGoal.text = [NSString stringWithFormat:@"%.1f", fToGol];
        
      bOK = [pApp saveGoalData:pApp.m_bodyInfo.m_nBodyId data:fGoalWgt dvUn:2 unitType:11 toSvr:YES] != nil;
      if(bOK) {
        bodyInfo.fGoalWgt = fGoalWgt;
        _lblGoalWgt.text = [NSString stringWithFormat:@"%.1f %@", fGoalWgt, (pApp.m_bodyInfo.nWgtUn == 1)?@"kg":@"kg"];
      }
    }
    else if(_lblLastInput == _lblTodayDists) {
        float fGoalDists = sTxtVal.floatValue;
        bOK = [pApp saveGoalData:pApp.m_bodyInfo.m_nBodyId data:fGoalDists dvUn:2 unitType:12 toSvr:YES] != nil;
        if(bOK) {
            bodyInfo.fGoalDist = fGoalDists;
            _lblTodayDists.text = [NSString stringWithFormat:@"%.1f mi",fGoalDists];
        }
    }
    else if(_lblLastInput == _lblGoalSteps) {
        int nGoalSteps = sTxtVal.intValue;
        bOK = [pApp saveGoalData:pApp.m_bodyInfo.m_nBodyId data:(float)nGoalSteps dvUn:0 unitType:13 toSvr:YES] != nil;
        if(bOK) {
            bodyInfo.nGoalStep = nGoalSteps;
            _lblGoalSteps.text = sTxtVal;
        }
    }

    if(bOK)
        [_txtDelegate CompleteTextInput];
    return bOK;
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
  bool bInputFat = theTextField==_txtFatInput;
  [theTextField resignFirstResponder];
  
  if(bInputFat) {
    theTextField.hidden = YES;
    float fFatVal = [self insertFatInputData:theTextField.text];
    _lblFatVal.hidden = NO;
    _lblFatVal.text = [NSString stringWithFormat:@"%0.1f%%", fFatVal];
  
    [_txtWgtInput becomeFirstResponder];
  }
  else {
    if(!_txtFatInput.hidden) {
      theTextField.hidden = YES;
      float fFatVal = [self insertFatInputData:_txtFatInput.text];
      _lblFatVal.hidden = NO;
      _lblFatVal.text = [NSString stringWithFormat:@"%0.1f%%", fFatVal];
    }
    BOOL bOK = [self insertGoalData:theTextField.text];
    [self setManualMode: NO];
    
    if(bOK &&  _lblLastInput == _lblMeasVal) {
      AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      float fWgtVal = theTextField.text.floatValue;
      _lblMeasVal.text = [NSString stringWithFormat:@"%0.1f", fWgtVal];
      _lblUnitTxt.text = (pApp.m_bodyInfo.nWgtUn == 1)? @"kg": @"kg";
      
      
      //[_ivBottom setFatWatBmiVal:fLfat wat:fLwat bmi:fBmi];
    }
  }
  
  return YES;
}

//test start
-(void) insertTestWgtFatData
{
    AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //[pApp removeCoreData:@"N_WgtData"];
    //return;
    
    NSString* sInitDtFmt = pApp.dtFmt.dateFormat;
    NSDateFormatter* dtFmt = pApp.dtFmt;
    [dtFmt setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    
    NSInteger nBodyId = pApp.m_bodyInfo.m_nBodyId;
    
    NSString* dtMeas = @"11/01/2015 10:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:132 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:11.0 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/02/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:138 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:16.0 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/03/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:142 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:21.2 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES]; 
    
    dtMeas = @"11/16/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:130.2 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:21.0 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/17/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:135.4 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:25.0 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/18/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:140.5 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:24.0 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/19/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:138.6 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:23.8 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/20/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:126.5 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:18.0 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/21/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:208.5 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:17.5 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    dtMeas = @"11/22/2015 11:23:45";
    [self insWgtData:@"TestSn" card:@"cardNo" data:158.9 unit:2 dtScale:dtMeas];
    [pApp insertFatData:nBodyId fat:20.8 wat:51.0 hgt:170 unit:1 oth:0l date:[dtFmt dateFromString:dtMeas] toSvr:YES];
    
    [pApp.dtFmt setDateFormat:sInitDtFmt];
}

- (BOOL) insWgtData:(NSString*)sSn card:(NSString*)cn data:(float)fData unit:(short)iUnit dtScale:(NSString*) sTime {
    Boolean biOsTime = NO;//[sn hasPrefix:@"RIS"];
    AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    return [pApp insertWgtData:sSn card:cn data:fData unit:iUnit bodyId:pApp.m_bodyInfo.m_nBodyId dtScale:sTime iOStime: biOsTime toSvr:YES] != NULL;
    //return YES;
}
//test end

- (void) hideOthCtrls:(BOOL)bHide
{
    _lblFatVal.hidden = (_nMeasTypeIdx==3)? NO: bHide;
  _lblGoalWgt.hidden = (_nMeasTypeIdx==3)? NO:bHide;
    _lblWgtLost.hidden = (_nMeasTypeIdx==3)? NO:bHide;
    _lblToGoal.hidden = (_nMeasTypeIdx==3)? NO:bHide;
}

-(void) resetMeasVals
{
    //_lblBodyBmi.text = @"...";
    _lblMeasVal.text = @"- - -";
    _lblFatVal.text = @"...";
}

- (void) setMeasVals:(float)fWgt fat:(float)fFat un:(short)nWu
{
  if(_hrmCircleLayer != nil) {
    [_hrmCircleLayer setFillColor:[UIColor clearColor].CGColor];
    //[_hrmCircleLayer removeFromSuperlayer];
  }
  
  if(_nMeasTypeIdx < 3) {
    if(fWgt > 0) {
      if(_nMeasTypeIdx == 1)
        _lblMeasVal.text = [NSString stringWithFormat:@"%.0f/%.0f", fWgt, fFat];
      else
        _lblMeasVal.text = [NSString stringWithFormat:@"%.0f", fWgt];
    }
    else {
        if (_nMeasTypeIdx == 0) {
            _lblMeasVal.text = @"---";
        } else if (_nMeasTypeIdx == 1) {
         _lblMeasVal.text = @"---/---";
        }
    }
    _lblUnitTxt.text = @"";
    
    /*CGRect rt = _lblMeasVal.frame;
   
    if(_nMeasTypeIdx == 0) {
      rt.origin.x += _bIpad?2:2;
    }
    if(_nMeasTypeIdx == 2) {
      rt.origin.x += _bIpad? 10:10;
      rt.origin.y -= 20;
    }
    else {
      rt.origin.x += _bIpad? 10:10;
    }*/
    //_lblMeasVal.frame = rt;
  }
  else {
    if(fWgt > 0)
      _lblMeasVal.text = [NSString stringWithFormat:@"%.1f", fWgt];
    else
      _lblMeasVal.text = @"---.-";
    if(fFat > 0)
      _lblFatVal.text = [NSString stringWithFormat:@"%d%%", (int)fFat];
    else
      _lblFatVal.text = @"--%";
    //wgt unit
    _lblUnitTxt.text = (nWu == 1)?@"kg": @"kg";
  }
  _sLastLabelTxt = _lblMeasVal.text;
  
  _nLastMeasWgtUn = nWu;
}

- (void) setHrateVals:(short)nHrate colIdx:(int)nIdx;
{
  
  //_ivRing.highlighted = YES;
  if(nHrate > 0) {
    if(nIdx == 0)
      _ivRing.image = [UIImage imageNamed:@"ring-hrate-0.png"];
    else if(nIdx == 1)
      _ivRing.image = [UIImage imageNamed:NSLocalizedString(@"ring-hrate-1", nil)];
    else if(nIdx == 2)
      _ivRing.image = [UIImage imageNamed:NSLocalizedString(@"ring-hrate-2", nil)];
    else if(nIdx == 3)
      _ivRing.image = [UIImage imageNamed:NSLocalizedString(@"ring-hrate-3", nil)];
    else if(nIdx == 4)
      _ivRing.image = [UIImage imageNamed:NSLocalizedString(@"ring-hrate-4", nil)];
    else if(nIdx == 5)
      _ivRing.image = [UIImage imageNamed:NSLocalizedString(@"ring-hrate-5", nil)];
  }
  else {
    _ivRing.image = [UIImage imageNamed:@"ring-hrate-0.png"];
  }
  //[_ivRing setNeedsDisplay];
  
  /*CGRect rt = self.frame;
  BOOL bIpad = rt.size.width > 500;
  BOOL bIp6 = rt.size.height > 381;
  
  UIColor* col = [UIColor clearColor];
  if(nHrate > 0) {
    if(nIdx == 0)
      col = [[UIColor grayColor]colorWithAlphaComponent:0.50];
    else if(nIdx == 1)
      col = [[UIColor blueColor]colorWithAlphaComponent:0.35];
    else if(nIdx == 2)
      col = [[UIColor greenColor] colorWithAlphaComponent:0.35];
    else if(nIdx == 3)
      col = [[UIColor yellowColor]colorWithAlphaComponent:0.35];
    else if(nIdx == 4)
      col = [[UIColor orangeColor] colorWithAlphaComponent:0.35];
    else if(nIdx == 5)
      col = [[UIColor redColor] colorWithAlphaComponent:0.35];
  }
  
  if(_hrmCircleLayer == nil) {
    CGFloat x = bIpad?228:(bIp6?112:87);
    CGFloat y = bIpad?146:(bIp6?125:105);
    CGFloat r = bIpad?275:(bIp6?115:105);
    _hrmCircleLayer = [CAShapeLayer layer];
    [_hrmCircleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, r, r)] CGPath]];
    [self.layer insertSublayer:_hrmCircleLayer below:self.layer];
  }
  [_hrmCircleLayer setFillColor:col.CGColor];*/
  
  if(nHrate > 0) {
      [UIView animateWithDuration:0.5 animations:^{
          _lblMeasVal.alpha = 0.0f;
          _lblMeasVal.text = [NSString stringWithFormat:@"%d", nHrate];
          _lblMeasVal.alpha = 1.0f;
      }];
    //_lblMeasVal.text = [NSString stringWithFormat:@"%d", nHrate];
  }
  else {
    _lblMeasVal.text = @"---";
  }
}

- (void) setWgtTips:(float)fGoalWgt wgtLost:(float)fWgtLost toGoal:(float)fToGoal wgtUn:(short)nWgtUn
{
  NSString* sWgtUn = (nWgtUn==2)?@"kg":@"kg";
  _lblGoalWgt.text = (fGoalWgt>0)?[NSString stringWithFormat:@"%.1f %@", fGoalWgt, sWgtUn]: @"---.-";
  _lblWgtLost.text = (fWgtLost!=0.0)?[NSString stringWithFormat:@"%.1f", fWgtLost]: @"---.-";
  _lblToGoal.text = (fToGoal!=0.0)?[NSString stringWithFormat:@"%.1f", fToGoal]: @"---.-";
}

- (void) setHrtHrtAvgTips:(short)nAvgHrt
{
    _lblAvgHrate.text = (nAvgHrt!=0)?[NSString stringWithFormat:@"%d", (short)(nAvgHrt+0.5)]: @"---";
}

- (void) setHrtBldBestTips:(short)nBestBldp
{
    _lblBestBldp.text = (nBestBldp!=0)?[NSString stringWithFormat:@"%d", nBestBldp]: @"---";
}

- (void) setHrtBldAvgTips:(short)nHgtBldp low:(short)nLowBldp
{
  NSString* sBLdps = nil;
  if(nHgtBldp > 0 && nLowBldp > 0)
    sBLdps = [NSString stringWithFormat:@"%d/%d", nHgtBldp, nLowBldp];
  else
    sBLdps = @"--/--";
  _lblBestBldp.text = sBLdps;
}

- (void) setStepsTips:(float)fTdists gStep:(float)fGsteps mStep:(int)nMsteps lStep:(int)nLsteps
{
    float fDist = fTdists;
    NSString* sFmt = @"%.1f mi";
    if(fDist < 0.001) {
        fDist*= 5280;
        sFmt = @"%.0f ft";
    }
    else if(fDist < 0.01) {
        sFmt = @"%.3f mi";
    }
    else if(fDist < 0.1) {
        sFmt = @"%.2f mi";
    }
  
  _lblTodayDists.text = (fTdists!=0)?[NSString stringWithFormat:sFmt, fDist]: @"----";
  _lblGoalSteps.text = (fGsteps!=0)?[NSString stringWithFormat:@"%.1f %%", fGsteps]: @"----";
  _lblMostSteps.text = (nMsteps!=0)?[NSString stringWithFormat:@"%d", nMsteps]: @"----";
  _lblMeasVal.text = (nLsteps != 0)?[NSString stringWithFormat:@"%d", nLsteps]: @"----";
}

- (void) setOtherValus:(float)fGoalWgt wgtLost:(float)fWgtLost toGoal:(float)fToGoal avgHrt:(short)nAvgHrt
                bestBldp:(short)nBestBldp todayDists:(float)fTdists gStep:(float)fGsteps mStep:(int)nMsteps
{
    _lblGoalWgt.text = (fGoalWgt>0)?[NSString stringWithFormat:@"%.1f kg", fGoalWgt]: @"---.-";
    _lblWgtLost.text = (fWgtLost!=0.0)?[NSString stringWithFormat:@"%.1f", fWgtLost]: @"---.-";
    _lblToGoal.text = (fToGoal!=0.0)?[NSString stringWithFormat:@"%.1f", fToGoal]: @"---.-";
    
    _lblAvgHrate.text = (nAvgHrt!=0)?[NSString stringWithFormat:@"%d", nAvgHrt]: @"---";
    _lblBestBldp.text = (nBestBldp!=0)?[NSString stringWithFormat:@"%d", nBestBldp]: @"---";
    
    _lblTodayDists.text = (fTdists!=0)?[NSString stringWithFormat:@"%.1f mi", fTdists]: @"----";
    _lblGoalSteps.text = (fGsteps!=0)?[NSString stringWithFormat:@"%.1f %%", fGsteps]: @"----";
    _lblMostSteps.text = (nMsteps!=0)?[NSString stringWithFormat:@"%d", nMsteps]: @"----";
}

- (void) setMeasIntVals:(int)nVal
{
    [UIView animateWithDuration:0.5 animations:^{
        _lblMeasVal.alpha = 0.0f;
        _lblMeasVal.text = [NSString stringWithFormat:@"%i", nVal];;
        _lblMeasVal.alpha = 1.0f;
    }];
    _sLastLabelTxt = _lblMeasVal.text;
}

- (void) setHrmMeasData:(short) nBmp
{
  [UIView animateWithDuration:0.5 animations:^{
    _lblMeasVal.alpha = 0.0f;
    _lblMeasVal.text = [NSString stringWithFormat:@"%i", nBmp];;
    _lblMeasVal.alpha = 1.0f;
  }];
}

- (void) updPedoBattery:(short)nLevel
{
  _lblPedoBattery.hidden = NO;
  _lblPedoBattery.text = (nLevel==0)?@"":[NSString stringWithFormat:@"Battery level: %i %%", nLevel];
    _lblPedoBattery.centerX = _lblMeasVal.centerX;
}

- (void) drawRing:(CGContextRef) ctx frame:(CGRect)rect
{
    CGContextAddEllipseInRect(ctx, rect);
    for(int i=1; i<20; i++) {
        CGRect rt =  CGRectInset(rect, i, i);
        UIColor* col = [UIColor colorWithRed: i/30.0 green:i/30.0 blue:i/30.0 alpha:1.0];
        CGContextAddEllipseInRect(ctx, rt);
    
        CGContextSetFillColor(ctx, CGColorGetComponents([col CGColor]));
        //CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
        CGContextEOFillPath(ctx);
    }
}

- (void) setMaualLabel
{
    _lblLastInput = _lblMeasVal;
}

- (void) hideKeyboard
{
  if(_txtFatInput.hidden && _txtWgtInput.hidden)
    return;
  
  _bCancel = YES;
  //if(_txtFatInput.isFirstResponder) {
    [_txtFatInput resignFirstResponder];
    //[self setManualMode:NO];
  //}
  
  
  [_txtWgtInput resignFirstResponder];
  [self setManualMode:NO];

  
  /*if(_txtWgtInput.isFirstResponder) {
    [_txtWgtInput resignFirstResponder];
    [self setManualMode:NO];
  }*/
}

-(void) setCtrlFontColor:(int)nIdx
{
  if(nIdx == 0) {
    _lblMeasVal.textColor = [UIColor colorWithRed:112.0/255 green:42.0/255 blue:135.0/255 alpha:1.0];
    _lblGoalWgt.textColor = [UIColor colorWithRed:112.0/255 green:42.0/255 blue:135.0/255 alpha:1.0];
    _lblFatVal.textColor =  [UIColor colorWithRed:112.0/255 green:42.0/255 blue:135.0/255 alpha:1.0];
  }
  else if(nIdx == 1) {
    _lblMeasVal.textColor = [UIColor colorWithRed:215.0/255 green:26.0/255 blue:117.0/255 alpha:1.0];
    //_lblFatVal.textColor = [UIColor colorWithRed:215.0/255 green:26.0/255 blue:117.0/255 alpha:0.75];
    
    _lblBestBldp.textColor = [UIColor colorWithRed:215.0/255 green:26.0/255 blue:117.0/255 alpha:1.0];

  }
  else if(nIdx == 2) {
    _lblMeasVal.textColor = [UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:1.0];
    _lblFatVal.textColor =  [UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:1.0];
    
    _lblTodayDists.textColor = [UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:1.0];
    _lblGoalSteps.textColor = [UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:1.0];
    _lblMostSteps.textColor  = [UIColor colorWithRed:0.0/255 green:144.0/255 blue:81.0/255 alpha:1.0];
  }
  else if(nIdx == 3) {
    _lblMeasVal.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
    _lblUnitTxt.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
    _lblGoalWgt.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
    _lblWgtLost.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
    _lblToGoal.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
    _lblFatVal.textColor = [UIColor colorWithRed:0.0/255 green:140.0/255 blue:189.0/255 alpha:1.0];
  }
}
@end
